package org.jeecg.modules.app.login.service.impl;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.app.login.service.AppLoginService;
import org.jeecg.modules.power.user.entity.YlToken;
import org.jeecg.modules.power.user.mapper.YlTokenMapper;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.app.login.service.impl
 * @Author: mi
 * @CreateTime: 2024-07-12 17:01
 * @Description:
 */
@Service
public class AppLoginServiceImpl implements AppLoginService {

    @Autowired
    private YlTokenMapper ylTokenMapper;//token表

    @Override
    @Transactional
    public Result<?> accountLogin(SysUser sysUser) {
        Map<String, Object> map = new HashMap<String, Object>();
        Date day = new Date();
        String token = 	JwtUtil.sign(sysUser.getId(),sysUser.getUsername());
        ylTokenMapper.delGasTokenByUserId(sysUser.getId());//清除其他token  根据用户id
        YlToken ylToken=new YlToken();
        ylToken.setToken(token);
        ylToken.setUserId(sysUser.getId());
        ylToken.setUpdateTime(day);
        ylToken.setType(1);//1、app BD运营端
        ylToken.setExpireTime(  DateUtils.addDaysToDate(day,7));//过期时间增加七天
        ylTokenMapper.insert(ylToken);
        map.put("token",token);
        return Result.OK(map);
    }
}
